<!DOCTYPE html>
<html>
	<head>
		<title>商品分类表-添加/修改</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<!-- 所有的 css js 资源 -->
		<link rel="stylesheet" href="https://unpkg.com/element-ui@2.13.0/lib/theme-chalk/index.css">
		<link rel="stylesheet" href="../../static/sa.css"> 
		<script src="https://unpkg.com/vue@2.6.10/dist/vue.js"></script>
		<script src="https://unpkg.com/element-ui@2.13.0/lib/index.js"></script>
		<script src="https://unpkg.com/http-vue-loader@1.4.2/src/httpVueLoader.js"></script>
		<script src="https://unpkg.com/jquery@3.4.1/dist/jquery.js"></script>
		<script src="https://www.layuicdn.com/layer-v3.1.1/layer.js"></script>
		<script src="../../static/sa.js"></script>
		<script src="../../static/kj/upload-util.js"></script>
		<style type="text/css">
			.td-img{width: 400px; height: 200px; cursor: pointer; vertical-align: middle;}
			.c-panel .el-form .c-label{width: 5em !important;}
			.c-panel .el-form .el-input{width: 400px;}
		</style>
	</head>
	<body>
		<div class="vue-box" :class="{sbot: id}" style="display: none;" :style="'display: block;'">
			<!-- ------- 内容部分 ------- -->
			<div class="s-body">
				<div class="c-panel">
					<div class="c-title">数据添加</div>
					<el-form v-if="m">
						<sa-item name="图片" br>
							<img :src="m.img_src" class="td-img" @click="sa.showImage(m.img_src, '70%', '80%')" >
							<el-link type="primary" @click="sa.uploadImage(src => {m.img_src = src; sa.ok2('上传成功');})">上传</el-link>
						</sa-item>
						<sa-item name="标题" v-model="m.title" br></sa-item>
						<sa-item name="排序" v-model="m.sort" type="num" br></sa-item>
						<sa-item name="" class="s-ok" br>
							<el-button type="primary" icon="el-icon-plus" @click="ok()">保存</el-button>
						</sa-item>
					</el-form>
				</div>
			</div>
			<!-- ------- 底部按钮 ------- -->
			<div class="s-foot">
				<el-button type="primary" @click="ok()">确定</el-button>
				<el-button @click="sa.closeCurrIframe()">取消</el-button>
			</div>
		</div>
		<script src="./mock-data-list.js"></script>
        <script>
			
			var app = new Vue({
				components: {
					"sa-item": httpVueLoader('../../sa-frame/com/sa-item.vue'),		
					"sa-info": httpVueLoader('../../sa-frame/com/sa-info.vue'),		
				},
				el: '.vue-box',
				data: {
					id: sa.p('id', 0),		// 获取超链接中的id参数（0=添加，非0=修改） 
					m: null,		// 实体对象 
				},
				methods: {
					// 创建一个 默认Model 
					createModel: function() {
						return {
							id: 11, 
							title: '标题',
							type: 1, 
							sort: 0,
							status:1, 
							img_src: 'http://color-test.oss-cn-qingdao.aliyuncs.com/dyc/img/2020/01/20/1579506071035455989950.jpeg',
							create_time: new Date(),
							click_count: 42,
							is_update: false,
						}
					},
					// 提交数据 
					ok: function(){
						// 验证 
						sa.checkNull(this.m.title, '请输入标题');
						sa.checkNull(this.m.sort, '请输入排序值');
						// 开始增加或修改
						// this.m.create_time = undefined;		// 不提交属性：创建日期
						if(this.id <= 0) {	// 添加
							sa.ajax2('/SysSwiper/add', this.m, function(res){
								sa.alert('增加成功', function() {
									if(parent.app) {
										var m2 = sa.copyJSON(this.m);
										parent.app.dataList.push(m2);
										parent.sa.f5TableHeight();		// 刷新表格高度 
										sa.closeCurrIframe();	// 关闭本页 
									} else {
										app.m = this.createModel();
									}
								}.bind(this)); 
							}.bind(this));
						} else {	// 修改
							sa.ajax2('/SysSwiper/update', this.m, function(res){
								sa.alert('修改成功', this.clean);
							}.bind(this));
						}
					},
					// 添加/修改 完成后的动作
					clean: function() {
						if(this.id == 0) {
							this.m = this.createModel();
						} else {
							parent.app.f5();		// 刷新父页面列表
							sa.closeCurrIframe();	// 关闭本页 
						}
					}
				},
				mounted: function(){
					// 初始化数据 
					if(this.id <= 0) {	
						this.m = this.createModel();
					} else {	
						sa.ajax2('/SysSwiper/getById?id=' + this.id, function(res) {
							this.m = res.data;
						}.bind(this), {res: getById(this.id)});
					}
				}
			})
			
			// 获取对应的data 
			function getById(id) {
				var res = {
					code: 200,
					msg: 'ok',
					data: null
				};
				mockDataList.data.forEach(function(item) {
					if(item.id == id) {
						res.data = item;
					}
				})
				return res;
			}
		</script>
	</body>
</html>